<?php

/*
 * yangzhic
 * 2016年8月17日下午6:50
 * OrderController
 */
namespace SDXApi\Controller;

use Think\Controller;
use Think\Model;

class MessageController extends AppController {
	
	
	/*
	 * 我的消息状态
	 */
	public function my_message_state() {
			$uid = I ( 'post.uid' );
			if (empty ( $uid )) {
				returnApiError ( 'uid 不存在' );
			}
			
			$where=array();
			$where['is_read']=0;
			$where['user_id']=$uid;
			
			
			$is_unread_count = M("message_log")->where ($where)->count();
			
			if ($is_unread_count) {
				returnApiSuccess ( array ('unread'=>$is_unread_count), '有未读消息');
			} else {
				returnApiError ( '无未读消息' );
			}
		
	}
	
	/*
	 * 我的消息列表
	 */
	public function my_message_list() {
	    $uid = I ( 'post.uid' );
	    if (empty ( $uid )) {
	        returnApiError ( 'uid 不存在' );
	    }
	    $current_page = I('post.page',1);   //页码
	    $start = ($current_page-1) *20;
	    $page_current = !empty($current_page) ? $current_page: 1;
	    	
	    $where=array();
	    $where['user_id']=$uid;
	    	
	    $message_list=array();
	    $list = M("message_log")->alias("ml")->join(" left join sdx_mail_templates as mt on ml.templates_id=mt.template_id left join sdx_alipush_activity as aa on mt.click_push_activity=aa.id")->where ($where)->order("send_time desc")->Field("mt.template_code,ml.order_sn,ml.consignee,ml.message_id,ml.send_time,mt.push_title,ml.send_content,ml.is_read,aa.ios_act,aa.ios_parameter,aa.android_act,aa.android_parameter,ml.web_url,mt.click_push_web_title,mt.click_push_act,ml.template_type,mt.ios_activity,mt.ios_param_key,mt.ios_param_val,mt.android_activity,mt.android_param_key,mt.android_param_val")->limit($start,20)->order("ml.send_time desc")->select();
	    $message_num = M('message_log')->where($where)->count();
	    foreach($list as $key=>$val){
	        empty($val['push_title'])?$list[$key]['push_title']='':$list[$key]['push_title']=$val['push_title'];
	        
	        if($val['template_type']!=1){
    	        empty($val['ios_act'])?$list[$key]['ios_act']='':$list[$key]['ios_act']=$val['ios_act'];
    	        empty($val['android_act'])?$list[$key]['android_act']='':$list[$key]['android_act']=trim($val['android_act']);
    	        $ios_parameter=unserialize($val['ios_parameter']);
    	        empty($ios_parameter)?$list[$key]['ios_parameter']=array():$list[$key]['ios_parameter']=$ios_parameter;
    	        $android_parameter=array();
    	        foreach(unserialize($val['android_parameter']) as $akey=>$aval){
    	            $android_parameter[]=$akey.':#'.$aval;
    	        }
    	        $list[$key]['android_parameter']=$android_parameter;
    	        
    	        if(!empty($val['web_url'])){
    	            $list[$key]['android_act']='com.weechan.shidexianapp.activity.WebActivity';
    	            if(!empty($val['click_push_web_title'])){
    	            $list[$key]['android_parameter']=array('INTENT_TITLE:#'.$val['click_push_web_title'],'com.weechan.shidexianapp:#'.urldecode($val['web_url']));
    	            }else{
    	                $list[$key]['android_parameter']=array('com.weechan.shidexianapp:#'.urldecode($val['web_url']));
    	            }
    	            
    	            $list[$key]['ios_act']='web';
    	            $list[$key]['ios_parameter']['web_title']=$val['click_push_web_title'];
    	            $list[$key]['ios_parameter']['web_url']=urldecode($val['web_url']);
    	        }else if(!empty($val['order_sn'])){
    	            $order_id=M("order_info")->where(array('order_sn'=>$val['order_sn']))->getField("order_id");
    	            $list[$key]['android_act']='com.weechan.shidexianapp.activity.OrderDetailActivity';
    	            $list[$key]['android_parameter']=array('com.weechan.shidexianapp:#'.$order_id);
    	             
    	            $list[$key]['ios_act']='orderDetail';
    	            $list[$key]['ios_parameter']['order_id']=$order_id;
    	        }else if(in_array($val['template_code'],array("Recharge_gift","activity_60_100_arrived"))){
    	            $list[$key]['android_act']=' com.weechan.shidexianapp.activity.BalanceActivity';
    	            $list[$key]['android_parameter']=array();
    	            
    	            $list[$key]['ios_act']='lastAcount';
    	            $list[$key]['ios_parameter']=array();
    	        }else if(in_array($val['template_code'],array("goods_arrive_favorite"))){
    	            $list[$key]['android_act']=' com.weechan.shidexianapp.TabHostActivity';
    	            $list[$key]['android_parameter']=array('COLLECT:#0');
    	            
    	            $list[$key]['ios_act']='myCollect';
    	            $list[$key]['ios_parameter']=array();
    	        }
	        
	        }else{
	            empty($val['ios_activity'])?$list[$key]['ios_act']='':$list[$key]['ios_act']=$val['ios_activity'];
	            empty($val['android_activity'])?$list[$key]['android_act']='':$list[$key]['android_act']=trim($val['android_activity']);
	            
	            $ios_param_key=$val['ios_param_key'];
	            $ios_param_val=$val['ios_param_val'];
	            (empty($ios_param_key) or empty($ios_param_val))?$list[$key]['ios_parameter']=array():$list[$key]['ios_parameter'][$ios_param_key]=$ios_param_val;
	            
	            $android_param_key=$val['android_param_key'];
	            $android_param_val=$val['android_param_val'];
	            (empty($android_param_key) or empty($android_param_val))?$list[$key]['android_parameter']=array():$list[$key]['android_parameter'][]=$android_param_key.':#'.$android_param_val;
	            
	            
	             
	            if(!empty($val['click_push_act'])){
	                $list[$key]['android_act']='com.weechan.shidexianapp.activity.WebActivity';
	                if(!empty($val['click_push_web_title'])){
	                    $list[$key]['android_parameter']=array('INTENT_TITLE:#'.$val['click_push_web_title'],'com.weechan.shidexianapp:#'.urldecode($val['click_push_act']));
	                }else{
	                    $list[$key]['android_parameter']=array('com.weechan.shidexianapp:#'.$val['click_push_act']);
	                }
	                 
	                $list[$key]['ios_act']='web';
	                $list[$key]['ios_parameter']['web_title']=$val['click_push_web_title'];
	                $list[$key]['ios_parameter']['web_url']=$val['click_push_act'];
	            }
	        }
	        
	        //empty($android_parameter)?$list[$key]['android_parameter']='':$list[$key]['android_parameter']=$android_parameter;
	    }
	    $message_list['list']=$list;
	    
	    $message_list['page_count']=ceil($message_num/20);
	    $message_list['count']=$message_num;
	    $message_list['page_current']=$page_current;
	    $message_list['is_bottom']=0;
	     
	    if($message_list['page_count']==$page_current){
	        $message_list['is_bottom']=1;
	    }
	    
	    M("message_log")->where ($where)->save(array('is_read'=>1)); 
	    if (!empty($message_list)) {
	        returnApiSuccess ($message_list, '我的消息列表');
	    } else {
	        returnApiError ( '无消息' );
	    }
	
	}
	
	
}